<?php

App::uses('AppController', 'Controller');

class RssController extends AppController {
    
    public $components = array('RequestHandler');
    public $layout     = 'rss';
    public $uses       = array('Group', 'Product', 'TasteGroup', 'Taste');
    
    public function afterFilter() {
        $this->RequestHandler->respondAs('xml');
    }
    
    public function index($groupId=null, $page=1) {
        
        if ($groupId) {
        	$data    = array();
        	$partial = 'products';
        	
        	if (preg_match('/^100/', $groupId)) {
        		$groupId = preg_replace('/^100/', '', $groupId);
        		$data = $this->Taste->find('all', array(
        				'conditions' => array(
        						'Taste.taste_group_id' => $groupId
        				),
        				'limit' => ITEMS_PER_PAGE,
        				'page'  => $page
        		));
        		$partial = 'tastes';
        		
        	} else if (preg_match('/^200/', $groupId)) {
        		$groupId = preg_replace('/^200/', '', $groupId);
	            $data = $this->Product->find('all', array(
	                'conditions' => array(
	                    'Product.group_id' => $groupId
	                ),
	                'limit' => ITEMS_PER_PAGE,
	                'page'  => $page
	            ));
        	}
            
            $this->set(compact('data'));
            $this->render($partial);
        } else {
            $groups = $this->Group->find('list');
			$tastes = $this->TasteGroup->find('list');
			
            $this->set(compact('groups', 'tastes'));
            $this->render('index');
        }
    }
    
}
